#pragma once

#include "qbe/src/state/state.hpp"
#include "qlib/all.h"

#include <entt/entt.hpp>

#include "../../ecs/query.hpp"

#include "../../select/select.hpp"

#include "./components.hpp"
namespace qbe::rotation {

enum struct State { None = 0, ManualRotation, AngleRotation, Wait, Clockwise90, NoClockwise90, Cancel };

struct Models {
  State state;

  bool first = true;

  bool waitManualRotaion = false;
  bool execute = false;

  bool self = true;
  double rotation = 0;

  bool need_dele_esc_one = false;

  state::SelectModeType selectMode;

  qlib::Vec2d manual_center_pos;

  bool setManualAngle(ecs::Query query) {
    auto& state_t = query.get_resource<state::State>();
    if (state_t.getEditingMode() == state::EditingModeType::Rotation) {
      return false;
    }

    state_t.switchMode(state::EditingModeType::Rotation);

    auto& cell = query.get_resource<qlib::Cell::s_ptr>();
    auto& reg = cell->reg;

    auto view = reg.view<select::SelectTag>();
    view.each([&reg](entt::entity e) { reg.emplace_or_replace<RotateTag>(e); });

    state = State::ManualRotation;

    return true;
  }

  void reset() {
    state = State::None;
    waitManualRotaion = false;
    execute = false;
    first = true;
    need_dele_esc_one = false;
  }
};

}  // namespace qbe::rotation